<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 供应商数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\SupplierService;
use think\Request;

class SupplierController extends VerifyController
{
    /**
     * @var \common\service\SupplierService
     */
    protected $supplierService;

    public function __construct()
    {
        parent::__construct();
        $this->supplierService = new SupplierService();
    }

    /**
     * 获取供应商列表
     * @param Request $request
     * @return \think\Response
     */
    public function select(Request $request)
    {
        $param = $request->param();
        $resParam = [];
        if (array_key_exists('param', $param)) {
            $resParam = $param['param'];
        }
        try {
            $resData = $this->supplierService->getSupplierList($request->getUserId(), $request->getTenantId(), $resParam);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查找供应商
     * @param Request $request
     * @return \think\Response
     */
    public function findSupplier(Request $request)
    {
        $param = $request->param();
        $resParam = [];
        if (array_key_exists('param', $param)) {
            $resParam = $param['param'];
        }
        $resData = $this->supplierService->getSupplier($resParam, $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 通过手机号搜索供应商
     * @param Request $request
     * @return \think\Response
     */
    public function getSupplierByPhone(Request $request)
    {
        $param = $request->param();
        $resData = $this->supplierService->getSupplierByPhone($request->getUserId(), $param['param']);
        return json(success_response('', $resData));
    }

    /**
     * 基本查询
     * @param Request $request
     * @return \think\Response
     */
    public function baseFind(Request $request)
    {
        $resData = $this->supplierService->baseFind($request->param()['param']['filter']);
        return json(success_response('OK', $resData));
    }

    /**
     * 创建
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function create(Request $request)
    {
        try {
            $resData = $this->supplierService->create($request->param(), $request->getTenantId());
            return json(success_response('OK', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取供应商和客户
     * @param Request $request
     * @return \think\Response
     */
    public function getSupplierCustomerList(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $resData = $this->supplierService->getSupplierCustomerList($param, $request->getTenantId());
        return json(success_response('OK', $resData));
    }

    /**
     * 根据名称搜索供应商
     * @param Request $request
     * @return \think\Response
     */
    public function getSupplierByName(Request $request)
    {
        $param = $request->param();
        $resData = $this->supplierService->getSupplierByName($request->getTenantId(), $param['param']);
        return json(success_response('', $resData));
    }
}